package cn.btks.commonserver.util.poi;

import com.deepoove.poi.data.CellRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.util.List;

public class DynamicTablePolicy extends DynamicTableRenderPolicy {
    // 人员填充数据所在行
    private int startRowIndex = 1;
    // 每行单元格数量
    private int cellNumber = 0;
    // 序号
    int number = 1;

    public DynamicTablePolicy(int startRowIndex, int cellNumber){
        this.startRowIndex = startRowIndex;
        this.cellNumber = cellNumber;
    }
    @Override
    public void render(XWPFTable table, Object data) {
        if (null == data) return;
        TableStyle rowStyle = new TableStyle();
        rowStyle.setAlign(STJc.CENTER);
        List<RowRenderData> detailData = (List<RowRenderData>) data;

        List<RowRenderData> teams = detailData;
        if (null != teams) {
            table.removeRow(startRowIndex);
            for (int i = 0; i < teams.size(); i++) {
                teams.get(i).setRowStyle(rowStyle);
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(startRowIndex);
                for (int j = 0; j < cellNumber; j++) insertNewTableRow.createCell();
                List<CellRenderData> cells = teams.get(i).getCells();
                copytTableRow(insertNewTableRow,table.getRow(0));
                cells.get(0).getCellText().setText(String.valueOf(number));
                MiniTableRenderPolicy.Helper.renderRow(table, startRowIndex, teams.get(i));
                number++;
                startRowIndex++;
            }
        }
    }

    private void copytTableRow(XWPFTableRow target, XWPFTableRow source) {
        // 复制样式
        target.getCtRow().setTrPr(source.getCtRow().getTrPr());
        // 复制单元格
        for (int i = 0; i < target.getTableCells().size(); i++) {
            copyTableCell(target.getCell(i), source.getCell(i));
        }
    }
    private void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
        // 列属性
        target.getCTTc().setTcPr(source.getCTTc().getTcPr());
        // 删除目标 targetCell 所有单元格
        for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
            target.removeParagraph(pos);
        }
        // 添加段落
        for (XWPFParagraph sp : source.getParagraphs()) {
            XWPFParagraph targetP = target.addParagraph();
            copyParagraph(targetP, sp);
        }
    }
    private void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
        // 设置段落样式
        target.getCTP().setPPr(source.getCTP().getPPr());
        // 添加Run标签
        for (int pos = 0; pos < target.getRuns().size(); pos++) {
            target.removeRun(pos);
        }
    }
}
